from domain.cellMap import CellMap


def values_has_zero(*values):
    """
    判断参数是否有0存在
    :param values: 参数
    :return: bool
    """
    for value in values:
        if float(value) == 0:
            return True
    return False


class CellResultMap:
    """
    单元结果对象
    """

    def __init__(self, cell_map: CellMap = None):
        # 接通率
        self.connection_rate = None
        # 未接通率
        self.unconnected_rate = None
        # 掉线率
        self.dropout_rate = None
        # 基站数量
        self.node_count = 0
        # rrc和e_rab有效基站数量
        self.rrc_and_e_rab_node_count = 0
        # 低接通小区数
        self.low_connection_cell_count = 0
        # 高掉线小区数
        self.high_dropout_cell_count = 0

        # 数据计算
        if cell_map is not None:
            self.calculate_connection_rate(cell_map)
            self.calculate_unconnected_rate(cell_map)
            self.calculate_dropout_rate(cell_map)

    def __str__(self):
        return str(f'接通率：{self.connection_rate} | 未接通率：{self.unconnected_rate} | 掉线率：{self.dropout_rate} | '
                   f'低接通小区数：{self.low_connection_cell_count} | 高掉线小区数：{self.high_dropout_cell_count}')

    def calculate_high_dropout_cell_count(self, cell_result_map):
        """
        高掉线小区数
        """
        cell_result_map: CellResultMap
        if cell_result_map.dropout_rate is None:
            return
        if cell_result_map.dropout_rate >= 0.01:
            self.high_dropout_cell_count += 1

    def calculate_low_connection_cell_count(self, cell_result_map):
        """
        低接通小区数
        """
        cell_result_map: CellResultMap
        if cell_result_map.connection_rate is None:
            return
        if cell_result_map.connection_rate <= 0.9:
            self.low_connection_cell_count += 1

    def add_node_count(self, num):
        """
        增加城市总基站数量
        :param num: 基站个数
        """
        self.node_count += num

    def add_rrc_node_count(self, num, cell_map: CellMap):
        """
        增加城市内RRC有效基站个数
        :param cell_map:
        :param num: 基站个数
        """
        if (cell_map.rrc_connect_complete_count.value == 0 and cell_map.rrc_connect_all_count.value == 0 and
                cell_map.e_rab_success_count.value == 0 and cell_map.e_rab_all_count.value == 0):
            return
        self.rrc_and_e_rab_node_count += num

    def calculate_connection_rate(self, cell_map: CellMap):
        """
        接通率
        """
        if values_has_zero(cell_map.rrc_connect_all_count.value, cell_map.e_rab_all_count.value):
            return None
        self.connection_rate = (cell_map.rrc_connect_complete_count.value / cell_map.rrc_connect_all_count.value) * (
                cell_map.e_rab_success_count.value / cell_map.e_rab_all_count.value)

    def calculate_unconnected_rate(self, cell_map: CellMap):
        """
        未接通率
        """
        if values_has_zero(cell_map.rrc_connect_all_count.value + cell_map.e_rab_all_count.value):
            return None
        self.unconnected_rate = ((cell_map.rrc_connect_all_count.value - cell_map.rrc_connect_complete_count.value) + (
                cell_map.e_rab_all_count.value - cell_map.e_rab_success_count.value)) / (
                                        cell_map.rrc_connect_all_count.value + cell_map.e_rab_all_count.value)

    def calculate_dropout_rate(self, cell_map: CellMap):
        """
        掉线率
        """
        if values_has_zero(cell_map.e_rab_release_success_count.value + cell_map.e_rab_release_err_count.value):
            return None
        self.dropout_rate = cell_map.e_rab_release_err_count.value / (
                cell_map.e_rab_release_success_count.value + cell_map.e_rab_release_err_count.value)

    def __getitem__(self, item):
        return getattr(self, item)

    def __setitem__(self, key, value):
        setattr(self, key, value)
